#!/bin/sh

bfile="/tmp/bookmarks.json"

prepare_bmarks ()
{
	jq -r '.. | objects | select(.type == "text/x-moz-place") | [.uri, .title] | @tsv' "$bfile" | tr '\t' ' '
}

uri=$(prepare_bmarks | dmenu -b -l 10 -i | cut -f1 -d' ')

if [ -z "$uri" ]; then
	echo 'Nothing selected' >&2
	exit 0
fi

echo "$uri" >&2

if [ -n "$(pgrep icecat)" ]; then
	echo 'Icecat running. Open new tab…' >&2
	icecat --new-tab "$uri"
	exit $?
fi

if [ -n "$(pgrep firefox)" ]; then
	echo 'Firefox running. Open new tab…' >&2
	firefox --new-tab "$uri"
	exit $?
fi

echo 'No web browsers available.' >&2
exit 1
